package com.ssh.admin.action;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ssh.common.log.LogFactory;
import com.ssh.common.log.LogName;
import com.ssh.common.util.JsonResult;
import com.ssh.common.util.Page;
import com.ssh.entity.Brand;
import com.ssh.manager.BrandManager;

@Controller
@ParentPackage("admin")
@Scope("prototype")
@Results(value={
@Result(name="success", location="/admin/brand/brandList.jsp"),
@Result(name="findBrandByPage", type="json", params={"root","page","excludeproperties","page\\.curPageNum,page\\.totalPageNum,page\\.totalCount," +
"page\\.numberPerPage,page\\.pageData\\[\\d+\\]\\.id,page\\.pageData\\[\\d+\\]\\.name,page\\.pageData\\[\\d+\\]\\.content,page\\.pageData\\[\\d+\\]\\.createDate"}),
@Result(name="updateBrand", type="json", params={"root","jr","excludeproperties","jr\\.code"}),
@Result(name="deleteBrand", type="json", params={"root","page","excludeproperties","page\\.curPageNum"}),
@Result(name="getBrandAll", type="json", params={"root","brands","excludeproperties","brands\\[\\d+\\]\\.id,brands\\[\\d+\\]\\.name"})
})
public class BrandAction extends ActionSupport{
	@Resource(name="brandManagerImpl")
	private BrandManager brandManager;
	
	private Brand brand;
	private List<Brand> brands;
	private Page page = new Page();
	private JsonResult jr=new JsonResult();
	private String isDesc;
	private String ids;
	
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public List<Brand> getBrands() {
		return brands;
	}
	public void setBrands(List<Brand> brands) {
		this.brands = brands;
	}
	public Brand getBrand() {
		return brand;
	}
	public void setBrand(Brand brand) {
		this.brand = brand;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public JsonResult getJr() {
		return jr;
	}
	public void setJr(JsonResult jr) {
		this.jr = jr;
	}
	public String getIsDesc() {
		return isDesc;
	}
	public void setIsDesc(String isDesc) {
		this.isDesc = isDesc;
	}
	
	public String goBrandList(){
		
		return "success";
	}
	
	/**
	 * 保存品牌
	 * @return
	 */
	public String saveBrand(){
		brand.setDel(0);
		brand.setCreateDate(new Timestamp(System.currentTimeMillis()));
		brandManager.saveOrUpdateBrand(brand);
		return "success";
	}
	
	/**
	 * 修改品牌信息
	 * @return
	 */
	public String updateBrand(){
		try {
			Brand brand2 = brandManager.getBrandById(brand.getId());
			brand2.setName(brand.getName());
			brand2.setContent(brand.getContent());
			brandManager.saveOrUpdateBrand(brand2);
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			LogFactory.getDefaultLog().error("品牌修改失败", LogName.API);
			e.printStackTrace();
		}
		return "updateBrand";
	}
	
	/**
	 * 删除品牌
	 * @return
	 */
	public String deleteBrand(){
		try {
			if(ids!=null && !ids.equals("")){
				String[] d = ids.split(",");
				for (int i = 0; i < d.length; i++) {
					brand = brandManager.getBrandById(new Long(d[i]));
					brand.setDel(1);
					brandManager.saveOrUpdateBrand(brand);
				}
			}else{
				brand = brandManager.getBrandById(brand.getId());
				brand.setDel(1);
				brandManager.saveOrUpdateBrand(brand);
			}
			
			int _p = (int) page.getCurPageNum();
			page.setNumberPerPage(20);
			page.setCurPageNum(1);
			brandManager.findBrandByPage(page, isDesc);
			if(_p<=page.getTotalPageNum()){
				page.setCurPageNum(_p);
			}else{
				page.setCurPageNum(_p-1);
			}
			jr.setCode("y");
		} catch (Exception e) {
			jr.setCode("n");
			LogFactory.getDefaultLog().error("品牌删除失败", LogName.API);
			e.printStackTrace();
		}
		return "deleteBrand";
	}
	
	/**
	 * 分页查询品牌
	 * @return
	 */
	public String findBrandByPage(){
		brandManager.findBrandByPage(page, isDesc);
		return "findBrandByPage";
	}
	
	/**
	 * 取出所有品牌信息
	 * @return
	 */
	public String getBrandAll(){
		brands = brandManager.getAllBrand();
		return "getBrandAll";
	}
}
